package com.dataextend.service;


import com.alibaba.fastjson.JSONObject;
import com.dataextend.entity.ShortMessageRequest;
import com.dataextend.mapper.cluster.MarketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 *  短信接口 模拟数据需对接腾讯云API
 */
@Service
public class ShortMessageService {
    @Autowired
    MarketMapper marketMapper;

    public JSONObject createGroup(ShortMessageRequest shortMessageRequest){
        // 当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = df.format(new Date());
        String creater = "管理员";

        if (shortMessageRequest.getPushPattern().equals("立即推送")){
            marketMapper.messageInfoInsert(shortMessageRequest.getGroupId(), shortMessageRequest.getGroupName(), shortMessageRequest.getMissionName(),
                    shortMessageRequest.getMessageInfo(), shortMessageRequest.getPushPattern(), creater, nowTime,
                    nowTime, shortMessageRequest.getPushEndTime());
        } else {
            marketMapper.messageInfoInsert(shortMessageRequest.getGroupId(), shortMessageRequest.getGroupName(), shortMessageRequest.getMissionName(),
                    shortMessageRequest.getMessageInfo(), shortMessageRequest.getPushPattern(), creater, nowTime,
                    shortMessageRequest.getPushStartTime(), shortMessageRequest.getPushEndTime());
        }


        // 插入一条该记录的模拟推送结果数据
        try {
            Random r = new Random();
            // 发送人数
            int targetNum = r.nextInt(200) + 2000;
            // 触达人数
            int sendNum = r.nextInt(100) + 1750;
            // 点击人数
            int clickNum = r.nextInt(50) + 300;
            // 订单金额
            int dealNum = r.nextInt(1000) + 13000;

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            long startDate_ = 0L;
            String startDate;
            String endDate;
            if (shortMessageRequest.getPushPattern().equals("立即推送")){
                startDate_ = simpleDateFormat.parse(nowTime).getTime();
                startDate = nowTime.substring(0,10);
                endDate = simpleDateFormat.format(new Date(startDate_ + 86400000*3));
                marketMapper.emailResultInsert(shortMessageRequest.getGroupId(), shortMessageRequest.getGroupName(), nowTime, nowTime,
                        startDate, endDate, targetNum, sendNum, clickNum, dealNum);
            } else {
                startDate_ = simpleDateFormat.parse(shortMessageRequest.getPushStartTime()).getTime();
                startDate = shortMessageRequest.getPushStartTime().substring(0,10);
                endDate = simpleDateFormat.format(new Date(startDate_ + 86400000*3));
                marketMapper.emailResultInsert(shortMessageRequest.getGroupId(), shortMessageRequest.getGroupName(), nowTime, shortMessageRequest.getPushStartTime(),
                        startDate, endDate, targetNum, sendNum, clickNum, dealNum);
            }

        } catch (Exception e){
            e.printStackTrace();
        }

        return new JSONObject();
    }

}
